package com.yudean.esb.common;

import com.yudean.esb.util.StringUtil;

/**
 * Created by zenghui on 2015-05-27.
 * desc:
 */
public class ServiceConstant {
    private static final String TAG = "ServiceConstant";

    // serviceType
    public static final String PUBLISH_SERVICE= "发布服务";
    public static final String EXECUTE_PUT_DATA = "写入数据";
    public static final String EXECUTE_DEL_DATA = "删除数据";


    // serviceName
    public static final String SERVICE_ERP_INSURANCE = "ERP-保险";
    public static final String SERVICE_DANGER_INT = "出险赔付信息";
    public static final String SERVICE_DANGER_TYPE = "险种信息";
    public static final String SERVICE_EMERGENCY_DATA = "安监报表信息";
    public static final String SERVICE_PRO_TECH_DATA = "年度申报项目";
    public static final String SERVICE_FINANCE_DATA = "ERP财务报表信息";

    // errorCode
    public static final String ERROR_CODE_BLANK_PARAMS = "blank params"; // 参数为空
    public static final String SUCCESS = "execute success"; // 执行成功
    public static final String ERROR_CODE_NO_DATA = "no data"; // 错误的参数

    //serviceID(针对生产项目管理系统和蓝科财务报表数据)
    public static final String SERVICE_ID = "erp_ids_protech";
    public static final String SERVICE_ID_APPR = "erp_ids_proappr";
    public static final String SERVICE_IDS = "erp_ids_protech,erp_ids_proappr";

    //查无数据
    public static final String NO_DATA = returnMsg(null,SERVICE_PRO_TECH_DATA,PUBLISH_SERVICE,null,null,null,null,"查无数据");


    /**
     * @return 权限认证失败返回的信息
     */
    public static String noPermission(String serviceName,String serviceType) {
        return returnMsg(null,serviceName,serviceType,null,null,null,null,"权限认证失败");
    }

    /**
     * 返回错误信息
     *
     * @param serviceID 定义的服务的唯一标示
     * @param token     安全令牌
     * @param result    错误结果
     * @param msg       错误信息详情
     * @return          String
     */
    public static String errorMsg(String serviceID, String token,
                                  String result, String msg) {
        return returnMsg(serviceID,null,null,token,null,null,result,msg);
    }

    /**
     * 推送数据成功或部分成功
     * @param serviceID 定义的服务的唯一标示
     * @param token     安全令牌
     * @param result    错误结果
     * @param msg       错误信息详情
     * @return          String
     */
    public static String okMsg(String serviceID, String token,Integer received,Integer submitted,
                               String result, String msg) {
        return returnMsg(serviceID,null,null,token,received,submitted,result,msg);
    }

    /**
     * 返回客户端无数据内容
     *
     * @param serviceID 定义的服务的唯一标示
     * @param token 安全令牌
     * @return XML noData
     */
    public static String noData(String serviceID, String token) {
        return errorMsg(serviceID, token, Constants.CODE_CLIENT_ERROR, "XML数据集为空!");
    }



    private static String returnMsg(String serviceID,String serviceName,String serviceType,String token,
                                    Integer received,Integer submitted, String result,String msg){
        StringBuilder sb = new StringBuilder();
        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><service>");
        if (!StringUtil.isEmpty(serviceID)){
            sb.append("<serviceID>").append(serviceID).append("</serviceID>");
        }
        if (!StringUtil.isEmpty(serviceName)){
            sb.append("<name>").append(serviceName).append("</name>");
        }
        if (!StringUtil.isEmpty(serviceType)){
            sb.append("<type>").append(serviceType).append("</type>");
        }
        if (!StringUtil.isEmpty(token)){
            sb.append("<token>").append(token).append("</token>");
        }
        if(received != null){
            sb.append("<received>").append(received).append("</received>");
        }
        if (submitted != null){
            sb.append("<submitted>").append(submitted).append("</submitted>");
        }
        if (!StringUtil.isEmpty(result)){
            sb.append("<result>").append(result).append("</result>");
        }
        sb.append("<msg>").append(msg).append("</msg></service>");
        return sb.toString();
    }
}
